package io;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

/**
 * Takes all the data and stores it into a archive.
 * Data is sorted as so:
 * +DATA TYPE
 *   - Name as defined by the Data's ID in the library.
 * @author G'lek
 * @version 0.0.1
 */
public class DataCompiler {
    
    public static void compileData(DataLibrary lib, String file) throws FileNotFoundException, IOException{
        ZipOutputStream output;
        FileOutputStream fileOut = new FileOutputStream(file);
        output = new ZipOutputStream(fileOut);
        OutputStreamWriter writer = new OutputStreamWriter(output);
        ArrayList<Data> data = lib.getAllData();
        for(Data d : data){
            ZipEntry e = new ZipEntry(lib.getID(d));
            output.putNextEntry(e);
            writer.write(d.toJSONString());
            writer.flush();
        }
        output.close();
        return;
    }
}
